<template>
	<view class="add_container">
		<view class="form_container">
			<view class="form_item">
				<view class="label">名称</view>
				<view class="item_input">
					<u--input placeholder="请填写需要开具发票的企业名称" border="none" v-model="formData.name" inputAlign="right"
						fontSize="12"
						placeholder-style="text-align: right;color: rgb(167, 167, 167);font-size: 24rpx;"></u--input>
				</view>
			</view>
			<view class="form_item">
				<view class="label">税号</view>
				<view class="item_input">
					<u--input placeholder="请填写纳税人识别号" border="none" v-model="formData.dutyNumber" inputAlign="right"
						fontSize="12"
						placeholder-style="text-align: right;color: rgb(167, 167, 167);font-size: 24rpx;"></u--input>
				</view>
			</view>
			<view class="form_item">
				<view class="label">开户银行</view>
				<view class="item_input">
					<u--input placeholder="请填写开户银行" border="none" v-model="formData.openBank" inputAlign="right"
						fontSize="12"
						placeholder-style="text-align: right;color: rgb(167, 167, 167);font-size: 24rpx;"></u--input>
				</view>
			</view>
			<view class="form_item">
				<view class="label">银行账号</view>
				<view class="item_input">
					<u--input placeholder="请填写银行账号" border="none" v-model="formData.bankAccount" type="number"
						inputAlign="right" fontSize="12"
						placeholder-style="text-align: right;color: rgb(167, 167, 167);font-size: 24rpx;"></u--input>
				</view>
			</view>
			<view class="form_item">
				<view class="label">公司地址</view>
				<view class="item_input">
					<view class="form_select" @click="selectAddress">
						<view style="color: #000;">{{formData.companyAddress}}</view>
					</view>
				</view>
			</view>
			<view class="form_item">
				<view class="label">公司电话</view>
				<view class="item_input">
					<u--input type="number" placeholder="请填写公司电话" border="none" v-model="formData.companyPhone"
						inputAlign="right" fontSize="12"
						placeholder-style="text-align: right;color: rgb(167, 167, 167);font-size: 24rpx;"></u--input>
				</view>
			</view>
		</view>
		<!-- 设为默认 -->
		<view class="default_container">
			<view class="title">设为默认</view>
			<view class="radio">
				<u-checkbox-group v-model="formData.isDefault" @change="defaultChange">
					<u-checkbox :name="true" shape="circle"></u-checkbox>
				</u-checkbox-group>
			</view>
		</view>
		<!-- 完成 -->
		<view class="complate" @click="complateHandle">完成</view>
	</view>
</template>

<script>
	import {
		addInvoice,
		getInvoiceInfo,
		editInvoice
	} from '../../../api/invoice.js'
	import {
		getDefaultAddress,
		getAddressInfo
	} from '@/api/address.js'
	export default {
		data() {
			return {
				backUrl: "",
				formData: {
					id: null,
					name: "",
					dutyNumber: "",
					openBank: "",
					bankAccount: "",
					companyAddress: "",
					companyPhone: "",
					isDefault: [false]
				},

			}
		},
		onLoad(option) {
			console.log("option", option);
			this.formData.isDefault = [false]
			this.backUrl = "";
			this.getDefaultAddress()
			uni.setStorage({
				key: 'invoiceType',
				data: "",
				success: function() {}
			});

			if (option.id) {
				let that = this
				uni.setStorage({
					key: 'invoiceId',
					data: option.id,
					success: function() {}
				});
				uni.getStorage({
					key: 'invoiceId',
					success: function(res) {
						that.formData.id = res.data
						that.getInfo()
					}
				});
			} else {
				this.formData.id = null;
			}

			// 添加发票模板，将内存id清空,存储返回的路径
			if (option.type == "addInvoiceTem") {
				uni.setStorage({
					key: 'invoiceId',
					data: "",
					success: function() {}
				});
				// uni.setStorage({
				// 	key: 'invoiceType',
				// 	data: "/pagesA/pages/invoiceTemplate/invoiceTemplate",
				// 	success: function() {
				// 		console.log('success');
				// 	}
				// });
			}
			// 申请发票添加新抬头，将invoiceId置空
			if (option.type == "addInvoice") {
				uni.setStorage({
					key: 'invoiceId',
					data: "",
					success: function() {
					}
				});
			}
			// 申请发票编辑开票信息和地址管理页面进来，从内存中取id值
			if (option.type == "editInvoice" || option.type == "companyAddress") {
				let that = this
				uni.getStorage({
					key: 'invoiceId',
					success: function(res) {
						that.formData.id = res.data
						that.getInfo()
					}
				});
			}

			// 从申请发票页面进来，存储其页面地址
			if (option.type == "editInvoice" || option.type == "addInvoice") {
				uni.setStorage({
					key: 'invoiceType',
					data: "/pagesA/pages/invoice/invoice",
					success: function() {
					}
				});
			}
			if (option.addressId) {
				this.getAddressInfo(option.addressId)
			}
		},
		onShow() {
			let _this = this;
			uni.getStorage({
				key: 'invoiceType',
				success: function(res) {
					_this.backUrl = res.data
				}
			});
		},
		methods: {
			prevDateFun(object) {
				let _this = this
				if (object) {
					_this.getAddressInfo(object.addressId)
				} else {
					return
				}
			},

			// 获取默认地址
			async getDefaultAddress() {
				const res = await getDefaultAddress()
				this.formData.companyAddress = res.province + res.city + res.disCoun.split(",")[0] + res.detaAddress
			},

			// 获取编辑数据
			async getInfo() {
				let list = {
					id: Number(this.formData.id)
				}
				const res = await getInvoiceInfo(list)
				this.formData.id = res.id
				this.formData.name = res.name
				this.formData.dutyNumber = res.dutyNumber
				this.formData.openBank = res.openBank
				this.formData.bankAccount = res.bankAccount
				this.formData.companyAddress = res.companyAddress
				this.formData.companyPhone = res.companyPhone
				this.formData.isDefault.push(res.isDefault)
				this.formData.isDefault[0] = res.isDefault
			},

			// 根据id获取地址信息
			async getAddressInfo(val) {
				let list = {
					id: Number(val)
				}
				const res = await getAddressInfo(list)
				this.formData.companyAddress = res.province + res.city + res.disCoun.split(",")[0] + res.detaAddress
			},

			// 更换地址
			selectAddress() {
				// 跳转到地址页面
				uni.navigateTo({
					url: `/pagesA/pages/address/address?type=addInvoice`
				})
			},

			//是否设置为默认
			defaultChange(val) {
				if (val.length > 0) {
					this.formData.isDefault[0] = true
				} else {
					this.formData.isDefault[0] = false
				}
			},

			//完成
			async complateHandle() {
				let arr = Object.values(this.formData)
				for (let i = 0; i < arr.length; i++) {
					if (arr[i] == "") {
						uni.showToast({
							title: '未填写完整',
							icon: 'error',
							duration: 1000,
						})
						return
					}
				}
				if (!this.formData.id) {
					let list = {
						name: this.formData.name,
						dutyNumber: this.formData.dutyNumber,
						openBank: this.formData.openBank,
						bankAccount: this.formData.bankAccount,
						companyAddress: this.formData.companyAddress,
						companyPhone: this.formData.companyPhone,
						isDefault: this.formData.isDefault[0]
					}
					const res = await addInvoice(list)
					if (res) {
						uni.showToast({
							title: '创建成功',
							icon: 'success',
							duration: 1000,
						})
						let _that = this
						setTimeout(() => {
							if (_that.backUrl) {
								uni.redirectTo({
									url: _that.backUrl + "?type=addInvoice"
								})
							} else {
								// 跳转到上一个页面
								uni.navigateBack({
									delta: 1
								});
							}
						}, 1000)
					} else {
						uni.showToast({
							title: '创建失败',
							icon: 'none',
							duration: 2000,
						})
					}
				} else {
					let list = {
						Id: this.formData.id,
						Name: this.formData.name,
						DutyNumber: this.formData.dutyNumber,
						OpenBank: this.formData.openBank,
						BankAccount: this.formData.bankAccount,
						CompanyAddress: this.formData.companyAddress,
						CompanyPhone: this.formData.companyPhone,
						IsDefault: this.formData.isDefault[0]
					}
					const res = await editInvoice(list)
					if (res) {
						uni.showToast({
							title: '修改成功',
							icon: 'success',
							duration: 1000,
						})
						let _that = this
						setTimeout(() => {
							if (_that.backUrl) {
								uni.redirectTo({
									url: _that.backUrl + "?type=addInvoice"
								})
							} else {
								// 跳转到上一个页面
								uni.navigateBack({
									delta: 1
								});
							}
						}, 1000)
					} else {
						uni.showToast({
							title: '更新失败',
							icon: 'none',
							duration: 2000,
						})
					}
				}
			}
		}
	}
</script>

<style lang="scss" scoped>
	.add_container {
		width: 100%;
		min-height: 100vh;
		background: rgb(245, 245, 245);
		padding: 34rpx 20rpx 170rpx 20rpx;
		padding-top: 34rpx;
		box-sizing: border-box;

		.form_container {
			width: 100%;
			background: rgb(255, 255, 255);
			border-radius: 8rpx;
			margin: 0 auto;

			.form_item {
				height: 94rpx;
				line-height: 94rpx;
				display: flex;
				justify-content: space-between;
				align-items: center;
				padding: 0 20rpx;
				box-sizing: border-box;

				.label {
					color: rgb(61, 61, 61);
					font-family: 思源黑体;
					font-size: 28rpx;
					font-weight: 400;
				}

				.item_input {
					width: 80%;

					.form_select {
						color: rgb(167, 167, 167);
						font-family: 思源黑体;
						font-size: 24rpx;
						font-weight: 400;
						text-align: right;
					}
				}
			}
		}

		.default_container {
			width: 100%;
			height: 104rpx;
			line-height: 104rpx;
			padding: 0 20rpx 0 26rpx;
			box-sizing: border-box;
			background: rgb(255, 255, 255);
			border-radius: 8rpx;
			display: flex;
			justify-content: space-between;
			align-items: center;
			margin-top: 40rpx;

			.title {
				color: rgb(61, 61, 61);
				font-family: 思源黑体;
				font-size: 28rpx;
				font-weight: 400;
			}

			.radio {
				width: 50rpx;
			}
		}

		.complate {
			width: 706rpx;
			height: 104rpx;
			line-height: 104rpx;
			text-align: center;
			margin: 0 auto;
			color: rgb(255, 255, 255);
			font-size: 40rpx;
			background: rgb(62, 86, 186);
			border-radius: 54rpx;
			position: fixed;
			left: 0;
			right: 0;
			bottom: 30rpx;
			z-index: 999;
		}
	}
</style>